//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.core.factory;

import java.util.Collections;

import org.apache.commons.lang3.Validate;
import org.jfab.core.collection.CGAnnotationList;
import org.jfab.core.collection.CGTypeList;
import org.jfab.core.collection.CommentList;
import org.jfab.core.collection.ModifierList;
import org.jfab.core.model.CGProperty;
import org.jfab.core.model.CGType;
import org.jfab.core.model.Modifier;

/**
 * Provides a property factory.
 */
public class DefaultCGPropertyFactory implements CGPropertyFactory
{
    /** String factory. */
    private final StringFactory _stringFactory;

    /**
     * Construct this object with the given parameter.
     * 
     * @param stringFactory String factory.
     */
    public DefaultCGPropertyFactory(final StringFactory stringFactory)
    {
        _stringFactory = stringFactory;
    }

    @Override
    public CGProperty createProperty(final CGType propertyType,
            final String propertyName)
    {
        final CGTypeList referencedTypes = null;
        final String comment = getStringFactory()
                .createCommentFromPropertyName(propertyName);
        final CommentList commentLines = new CommentList(
                Collections.singletonList(comment));
        final CGAnnotationList annotations = null;
        final ModifierList modifiers = new ModifierList(Modifier.PRIVATE);
        final String initialValue = null;
        final Boolean isGetter = null;
        final Boolean isSetter = null;
        final Boolean isUsedInIdentity = Boolean.TRUE;

        return createProperty(referencedTypes, commentLines, annotations,
                modifiers, propertyType, propertyName, initialValue, isGetter,
                isSetter, isUsedInIdentity);
    }

    @Override
    public final CGProperty createProperty(final CGTypeList referencedTypes,
            final CommentList commentLines, final CGAnnotationList annotations,
            final ModifierList modifiers, final CGType propertyType,
            final String propertyName, final String initialValue,
            final Boolean isGetter, final Boolean isSetter,
            final Boolean isUsedInIdentity)
    {
        Validate.notEmpty(propertyName, "propertyName is empty");

        final CGProperty answer = new CGProperty();

        if (referencedTypes != null)
        {
            answer.getReferencedTypes().addAll(referencedTypes);
        }

        if (commentLines != null)
        {
            answer.getCommentLines().addAll(commentLines);
        }

        if (annotations != null)
        {
            answer.getAnnotations().addAll(annotations);
        }

        if (modifiers != null)
        {
            answer.getModifiers().addAll(modifiers);
        }

        if (propertyType != null)
        {
            answer.setType(propertyType);
        }

        answer.setName(propertyName);

        if (initialValue != null)
        {
            answer.setInitialValue(initialValue);
        }

        if (isGetter != null)
        {
            answer.setGetter(isGetter);
        }

        if (isSetter != null)
        {
            answer.setSetter(isSetter);
        }

        if (isUsedInIdentity != null)
        {
            answer.setUsedInIdentity(isUsedInIdentity);
        }

        return answer;
    }

    /**
     * @return the stringFactory
     */
    private StringFactory getStringFactory()
    {
        return _stringFactory;
    }
}
